﻿using System.Collections.Generic;
using System;

namespace Ex03.GarageLogic
{
    public abstract class Motorcycle : Vehicle
    {
        public enum eLicenseType
        {
            A1, 
            A2, 
            A, 
            B
        }

        private const string k_LicenseTypeKey = "License Type";
        private const string k_CapacityKey = "Capacity";
        private const int k_NumberOfWheels = 2;
        private const float k_MaximumAirPressure = 28.0f;

        private eLicenseType m_LicenseType;
        private int m_Capacity;

        protected Motorcycle()
        {
            LicenseType = eLicenseType.A1.ToString();
            Capacity = 0.ToString();
        }

        public override void ParseFields(Dictionary<string, string> i_Form)
        {
            base.ParseFields(i_Form);

            string licenseTypeStr;
            if (!i_Form.TryGetValue(k_LicenseTypeKey, out licenseTypeStr))
            {
                throw new KeyNotFoundException(k_LicenseTypeKey);
            }

            string capacityStr;
            if (!i_Form.TryGetValue(k_CapacityKey, out capacityStr))
            {
                throw new KeyNotFoundException(k_CapacityKey);
            }

            LicenseType = licenseTypeStr;
            Capacity = capacityStr;
            Wheels = ParseWheels(i_Form, k_NumberOfWheels, k_MaximumAirPressure);
        }

        public override Dictionary<string, string> CreateForm()
        {
            Dictionary<string, string> form = base.CreateForm();
            form.Add(k_LicenseTypeKey, null);
            form.Add(k_CapacityKey, null);
            Wheel.CreateForm(ref form, k_NumberOfWheels);

            return form;
        }
        
        public string LicenseType
        {
            get { return m_LicenseType.ToString(); }
            set 
            {
                if (!Enum.IsDefined(typeof(eLicenseType), value))
                {
                    throw new FormatException(k_LicenseTypeKey);
                }

                try 
                {
                    m_LicenseType = (eLicenseType)Enum.Parse(typeof(eLicenseType), value);
                }
                catch(ArgumentException)
                {
                    throw new ArgumentException(k_LicenseTypeKey);
                } 
            }
        }

        public string Capacity
        {
            get { return m_Capacity.ToString(); }
            set
            {
                try
                {
                    m_Capacity = int.Parse(value);
                }
                catch (FormatException)
                {
                    throw new FormatException(k_CapacityKey);
                }
            }
        }

        public override string ToString()
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            stringBuilder.Append(base.ToString());
            stringBuilder.Append("License Type: ");
            stringBuilder.AppendLine(System.Enum.GetName(typeof(eLicenseType), m_LicenseType).ToString());
            stringBuilder.Append("Engine Capacity: ");
            stringBuilder.AppendLine(Capacity.ToString());

            return stringBuilder.ToString();
        }
    }
}
